import pyautogui
import time
from 检测框模块 import get_window_screenshot

def find_class_and_perform_action(hwnd, model, target_class, action, conf=0.6):
    found_target = False
    while not found_target:
        # 获取窗口截图
        screenshot = get_window_screenshot(hwnd)
        # 进行目标检测，并设置置信度阈值
        results = model(screenshot, conf=conf)
        for result in results:
            boxes = result.boxes.cpu().numpy()
            for box in boxes:
                class_id = int(box.cls[0])
                confidence = box.conf[0]
                if class_id == target_class and confidence >= conf:
                    found_target = True
                    break
            if found_target:
                break
        if not found_target:
            # 执行指定动作
            action()
            time.sleep(0.01)
    return found_target